#pragma once

#ifndef MAP_H
#define MAP_H

#include "common.h"
#include "frame.h"
#include "mappoint.h"

namespace myslam
{
    /**
     * @brief 地图
     * 和地图交互：前端调用inserKeyframe和insertMapPoint插入新颖和地图点，
     * 后端维护地图结构，判定outlines/剔除等等
     */
    
    class Map{
    public:
        EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
        typedef std::shared_ptr<Map> Ptr;
        typedef std::unordered_map<unsigned long, MapPoint::Ptr> LandmarksType;
        typedef std::unordered_map<unsigned long, Frame::Ptr> KeyframesType;
        
        Map() {}
        
        //增加一个关键帧
        void InsertKeyFrame(Frame::Ptr frame);
        //增加一个地图顶点
        void InsertMapPoint(MapPoint::Ptr map_point);
        
        //获取所有地图点
        LandmarkType GetAllMapPoints()
        {
            std::unique_lock<std::mutex> lck(data_mutex_);
            return Landmarks_;
        }
        
        //获取关键帧
        KeyframesType GetAllKeyFrames()
        {
            std::unique_lock<std::mutex> lck(data_mutex_);
            return Keyframes_;
        }
        
        //获取激活地图点
        LankmarksType GetActiveMapPoints()
        {
            std::unique_lock<std::mutex> lck(data_mutex_);
            return active_keyframes;
        }
        
        //清理map中的观测数量为零的点
        void CleanMap();
        
    private:
        //将旧的关键帧设置为不活跃状态
        void RemoveOldKeyframe();
        
        std::mutex data_mutex_;
        LandmarksType landmarks_;  //all landmarks
        LandmarksType active_landmarks_; //active landmarks
        KeyframesType Keyframes_;   //all key-frames
        KeyframesType active_keyframes_; // active key-frames
        
        Frame::Ptr current_frame_=nullptr;
        
        //settings
        int num_active_keyframes_=7;  //激活的关键帧的数量
    }；

    
}  //namespace myslam

#endif  //MAP_H

